package com.controller.recycler;

import com.alibaba.fastjson.JSONObject;
import com.service.HousingEstateService;
import com.utils.BaseController;
import com.utils.DataTables;
import com.entity.HousingEstate;
import com.entity.Scrap;
import com.entity.ScrapCollector;
import com.service.ScrapCollectorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 *  废品回收员 Controller
 */
@Controller
@RequestMapping("/scrapCollection")
public class RecyclerController extends BaseController{

    //定义全局变量存储省市信息
    String ss=null;

    @Autowired
    HttpServletRequest request;// 请求

    @Autowired
    private ScrapCollectorService scrapCollectorService;

    @Autowired
    private HousingEstateService housingEstateService;

    /**
     * 获取回收员列表
     * @since 2016年8月11日 下午9:02:02
     * @return
     */
    @RequestMapping(value = "/getPageList.do", produces = "application/json; charset=utf-8")
    @ResponseBody
    public String getPageList() {
        return JSONObject.toJSONString(scrapCollectorService.getPageList(DataTables.getInstance(request, null)));
    }


    /**
     * 显示所有
     *
     * @return
     */
    @RequestMapping(value = "/selectScrapCollector",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String selectScrapCollector() {
        return JSONObject.toJSONString(scrapCollectorService.selectScrapCollector());
    }

    /**
     * 删除
     * @since 2016年8月12日 下午8:42:07
     * @param idlist
     * @return
     */
    @RequestMapping(value = "/del.do",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String delScrap(@RequestParam("idlist[]") List<Integer> idlist) {
        return JSONObject.toJSONString(scrapCollectorService.delScrapCollector(idlist));
    }

    /**
     * 显示详情
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/getScrapById.do", produces = "application/json; charset=utf-8")
    @ResponseBody
    public String getScrapCollectorById(Integer id) {
        //根据id获取回收员对象
        ScrapCollector scrapColl = scrapCollectorService.getScrapCollectorById(id);
        scrapColl.setSs(ss);
        //回收员的负责的小区code
        String housCode = scrapColl.getHousingEstateCode();

        //判断小区code是否为空
        if (housCode.length()!=0){
            //根据code获取省市小区
            HousingEstate housingEstate = housingEstateService.selectHousingEstateByCode(housCode);

            //根据code获取省市信息
            String privince = housingEstateService.getPrivinceByCode(housingEstate.getProvinceCode());
            //根据cityCode获取城市名
            String city = housingEstateService.getCityByCode(housingEstate.getCityCode());
            //拼接省市名字
            String pc=privince+"-"+city;
            //设置省市名字
            scrapColl.setSs(pc);
            scrapColl.setHecName(housingEstate.getName());
            return JSONObject.toJSONString(scrapColl);
        }else{
            return JSONObject.toJSONString(scrapColl);
        }
    }

    /**
     * 添加
     *
     * @param scrapCollector
     * @return
     */
    @RequestMapping(value = "/add.do", produces = "application/json; charset=utf-8")
    @ResponseBody
    public String addScrap(@ModelAttribute ScrapCollector scrapCollector) {
        scrapCollector.setOpenid((int)(Math.random()*(9999-1000+1))+1000+"");
        scrapCollector.setIncome((long) ((int)(Math.random()*(9999-1000+1))+1000));
        scrapCollector.setCreateTime(new Date());
        //获取到idcard信息,根据idcard获取ss信息
        ss=scrapCollector.getSs();
        return JSONObject.toJSONString(scrapCollectorService.addScrapCollector(scrapCollector));
    }

    /**
     * 更新
     *
     * @param scrapCollector
     * @return
     */
    @RequestMapping(value = "/update.do", produces = "application/json; charset=utf-8")
    @ResponseBody
    public String updateScrap(@ModelAttribute ScrapCollector scrapCollector) {
        return JSONObject.toJSONString(scrapCollectorService.updateScrapCollector(scrapCollector));
    }


    /**
     * 查询所有
     *
     * @return
     */
    @RequestMapping(value = "/selectAllArea.do", produces = "application/json; charset=utf-8")
    @ResponseBody
    public String selectAllArea(){
        return JSONObject.toJSONString(scrapCollectorService.selectAllArea());
    }


    /**
     * 根据城市code获取小区
     *
     * @param cityCode
     * @return
     */
    @RequestMapping(value = "/getHousingEstateByCityCode.do", produces = "application/json; charset=utf-8")
    @ResponseBody
    public String getHousingEstateByCityCode(String cityCode){
        return JSONObject.toJSONString(scrapCollectorService.getHousingEstateByCityCode(cityCode));
    }
}
